package com.qifenqian.turbot.module.adapter.order

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.chad.library.adapter.base.entity.MultiItemEntity
import com.qifenqian.turbot.R
import com.qifenqian.turbot.module.entity.response.order.OrderDetail
import com.qifenqian.turbot.module.entity.response.order.OrderListEntity
import kotlinx.android.synthetic.main.item_order_orders.view.*
import kotlinx.android.synthetic.main.item_order_orders_header.view.*

/**
 * @author H
 * @createTime 2019/1/10 14:48
 * @description
 * @see
 * @since
 */
const val LEVEL_1 = 0x30
const val LEVEL_2 = 0x40

class OrderOrdersAdapter(data: List<MultiItemEntity>) : BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder>(data) {

    init {
        addItemType(LEVEL_1, R.layout.item_order_orders_header)
        addItemType(LEVEL_2, R.layout.item_order_orders)
    }

    override fun convert(helper: BaseViewHolder, item: MultiItemEntity) {
        when (helper.itemViewType) {
            LEVEL_1 -> {
                val view = helper.itemView
                item as OrderListEntity
                view.tv_item_order_orders_header_time.text = item.time
                view.btn_item_order_orders_header_fold.setTextColor(mContext.resources.getColor(if (item.isExpanded) R.color.red_fe4d3d else R.color.gray_666666))
                view.btn_item_order_orders_header_fold.setOnClickListener {
                    if (item.isExpanded) {
                        collapse(helper.adapterPosition)
                    } else {
                        expand(helper.adapterPosition)
                    }
                }
            }
            LEVEL_2 -> {
                val view = helper.itemView
                item as OrderDetail
                var bgRes: Int = -1
                var textColor: Int = -1
                var state: String = ""
                when (item.state) {
                    else -> {
                        bgRes = R.mipmap.bg_using
                        textColor = R.color.gray_666666
                        state = "就餐中"
                    }
                }
                view.tv_item_order_orders_table_number.text = item.tableNo
                view.tv_item_order_orders_people_count.text = "${item.minPeopleNumber}-${item.maxPeopleNumber}人"
                view.ly_item_order_orders_table.setBackgroundResource(bgRes)
                view.tv_item_order_orders_time.text = item.time
                view.tv_item_order_orders_discount.text = "${item.discountMoney}元"
                view.tv_item_order_orders_order_money.text = "${item.orderMoney}元"
                view.tv_item_order_orders_real_money.text = "${item.realMoney}"
                view.tv_item_order_orders_state.text = state
                view.tv_item_order_orders_state.setTextColor(textColor)
                view.tv_item_order_orders_real_money.text
                view.setOnClickListener {
                    if (mOnChildClickListener != null) {
                        mOnChildClickListener!!.onClick(helper.adapterPosition)
                    }
                }
            }
        }
    }

    private var mOnChildClickListener: OnChildClickListener? = null

    fun setOnChildClickListener(listener: OnChildClickListener) {
        mOnChildClickListener = listener
    }

    interface OnChildClickListener {
        fun onClick(position: Int)
    }
}
